import 'package:hinge/common/models/perfume_model.dart';

enum MessageType { text, perfumeCard }

class ChatMessageModel {
  final String id;
  final String chatId; // 所属聊天ID（群聊或单聊的ID）
  final int senderId; // 发送者ID
  final MessageType type; // 消息类型
  final String? text; // 文本内容（当type为text时使用）
  final PerfumeModel? perfumeCard; // 香水卡片（当type为perfumeCard时使用）
  final DateTime createdAt; // 创建时间

  ChatMessageModel({
    required this.id,
    required this.chatId,
    required this.senderId,
    required this.type,
    this.text,
    this.perfumeCard,
    required this.createdAt,
  });

  factory ChatMessageModel.fromJson(Map<String, dynamic> json) =>
      ChatMessageModel(
        id: json['id'],
        chatId: json['chatId'],
        senderId: json['senderId'],
        type: MessageType.values.byName(json['type']),
        text: json['text'],
        perfumeCard:
            json['perfumeCard'] != null
                ? PerfumeModel.fromJson(json['perfumeCard'])
                : null,
        createdAt: DateTime.parse(json['createdAt']),
      );

  Map<String, dynamic> toJson() => {
    'id': id,
    'chatId': chatId,
    'senderId': senderId,
    'type': type.name,
    'text': text,
    'perfumeCard': perfumeCard?.toJson(),
    'createdAt': createdAt.toIso8601String(),
  };
}
